#include "TeamMatchesMenu.h"
#include "ServiceProvider.h"
#include "StringUtilities.h"
#include "PrintUtilities.h"
#include "TeamManager.h"
#include "MenuManager.h"
#include "MatchMenu.h"
#include "LeagueManager.h"
#include "League.h"
#include "MatchData.h"
#include "GamePrintUtils.h"
#include "Types.h"

TeamMatchesMenu::TeamMatchesMenu(MenuManager* const i_menuManager)
	: Menu(i_menuManager)
	,m_teamID(Utilities::INVALID_ID)
{
}

void TeamMatchesMenu::OnEnter()
{
	const Team* const team = GetSP()->GetTeamMgr().GetTeam(m_teamID);
	const League* const league = GetSP()->GetLeagueMgr().GetLeagueFromID(team->GetLeagueID());

	GetSP()->GetLeagueMgr().GetTeamMatches(m_teamID, m_teamMatches);
	m_teamMatches.resize(GetSP()->GetLeagueMgr().GetAllLayers().at(league->GetLayerIndex())->MaxTurnsOfSeason(), 0);
}

void TeamMatchesMenu::Draw() const
{
	const Team* const team = GetSP()->GetTeamMgr().GetTeam(m_teamID);
	const League* const league = GetSP()->GetLeagueMgr().GetLeagueFromID(team->GetLeagueID());

	STD::cout << Print::Title("TEAM MATCHES MENU'");
	STD::cout << "Current League: " << league->GetName() << STD::endl << STD::endl;
	PrintCalendar(STD::cout, *team, GetSP());

	STL::string options("1-"+Utilities::IntToStlString(m_teamMatches.size())+") View Match.\n");
	options += "\n0) Return to previous menu'.";
	STD::cout << Print::Options(options);
}

bool TeamMatchesMenu::Input(const STL::string& i_input)
{
	bool goodInput = true;

	int res = Utilities::StringToInt(i_input);

	if (res > 0 && res <= static_cast<int>(m_teamMatches.size()))
	{
		MatchMenu* nextMenu = static_cast<MatchMenu*>(GetMM()->GetMatchMenu());
		nextMenu->SetMatch(m_teamMatches[res-1]);
		GetMM()->PushMenu(nextMenu);
	}
	else if (res == 0)
	{
		GetMM()->PopMenu();
	}
	else
	{
		goodInput = false;
	}

	return goodInput;
}
